﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using csharp.rad.general;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.GOVhotel
{
    public partial class HistoMensal : KryptonForm
    {
        Database myDBase;
        public static String Ação;
        string[] tabAções = { "nada", "faxina", "pós-saída", "atendimento",
            "dispensada", "limpeza", "reposição", "manutenção"};
        Cameiras rolCames;
        DateTime datada;
        int difData, progresso, nmPag = 1, qtItens, qRow;

        public HistoMensal(Database myDBase)
        {
            InitializeComponent();
            this.myDBase = myDBase;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            processBar.Top = 0;
            processBar.Visible = true;
            rolCames = new Cameiras(myDBase);
            int dbRet = rolCames.Lista();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                    + "referência: HistoMensalCames" + "\n" + rolCames.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            datada = Convert.ToDateTime("01/" + DateTime.Today.ToString("MM/yyyy"));
            Starta.Enabled = true;
        }

        private void Monta_Lançto()
        {
            menuMêsAno.Text = datada.ToString("MMMM/yyyy");
            listaMovto.Rows.Clear();
            listaMovto.Refresh();
            string antBloco = "X";
            Operações rolOpera = new Operações(myDBase);
            rolOpera.DataIni = datada;
            rolOpera.DataFim = datada.AddMonths(1);
            int dbRet = rolOpera.ListaMêsBloco();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                    + "referência: HistoMensalOper" + "\n" + rolOpera.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int[] totRepo = new int[] { 0, 0, 0, 0 };
            for (int ix = 0; ix < rolOpera.Items.Count; ix++)
            {
                string qApto =rolOpera.Items[ix].Local.ToString();
                if (antBloco != qApto[0].ToString())
                {
                    if (antBloco != "X")
                    {
                        listaMovto.Rows.Add(antBloco + "999999", "--", "----", "---------- ", "total", 
                            totRepo[0].ToString(), totRepo[1].ToString(), totRepo[2].ToString(), totRepo[3].ToString());
                        listaMovto.Rows[listaMovto.Rows.Count - 1].DefaultCellStyle.BackColor = SystemColors.Info;    
                        totRepo[0] = 0; totRepo[1] = 0; totRepo[2] = 0; totRepo[3] = 0;
                    }
                    antBloco = qApto[0].ToString();
                    string denomBlk = "Bloco " + antBloco;
                    if (antBloco == "0") denomBlk = "piscina";
                    else if (antBloco == "6") denomBlk = "limpeza";
                    listaMovto.Rows.Add(antBloco + "000000", " ", " ", denomBlk, " ", " ", " ", " ", " ");

                }
                string conjunto = "";
                int[] cjCame = new int[] { rolOpera.Items[ix].Cameira_1, rolOpera.Items[ix].Cameira_2,
                    rolOpera.Items[ix].Cameira_3, rolOpera.Items[ix].Cameira_4,
                    rolOpera.Items[ix].Cameira_5, rolOpera.Items[ix].Cameira_6 };
                for (int sq = 0; sq < 6; sq++)
                {
                    if (cjCame[sq] != 0)
                    {
                        for (int cm = 0; cm < rolCames.Items.Count; cm++)
                        {
                            if (rolCames.Items[cm].Código == cjCame[sq])
                            {
                                conjunto = conjunto + rolCames.Items[cm].Nome.Trim() + " / ";
                                break;
                            }
                        }
                    }
                }
                conjunto = conjunto.Remove(conjunto.Length - 3);
                Reposições rolRepo = new Reposições(myDBase);
                rolRepo.Número = rolOpera.Items[ix].Número;
                dbRet = rolRepo.Lista();
                if (dbRet != 0)
                {
                    MessageBox.Show("Favor avisar a RamaSoft" + "\n"
                        + "referência: HistoMensalRepo" + "\n" + rolRepo.MsgErro,
                        "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                int [] qtRepo = new int [4] { 0, 0, 0, 0 };
                for (int rp = 0; rp < rolRepo.Items.Count; rp++)
                { qtRepo [rolRepo .Items [rp].CódProduto -1]=rolRepo .Items [rp].Quantidade ; }
                listaMovto.Rows.Add(antBloco + rolOpera.Items[ix].Data.Day.ToString("00") + rolOpera.Items[ix].Local.ToString(),
                    rolOpera.Items[ix].Data.Day.ToString(), rolOpera.Items[ix].Local.ToString(),
                    conjunto, tabAções[rolOpera.Items[ix].Ação], qtRepo[0].ToString(), qtRepo[1].ToString(),
                    qtRepo[2].ToString(), qtRepo[3].ToString());
                totRepo[0] = totRepo[0] + qtRepo[0]; totRepo[1] = totRepo[1] + qtRepo[1];
                totRepo[2] = totRepo[2] + qtRepo[2]; totRepo[3] = totRepo[3] + qtRepo[3];
                progresso = progresso + 1;
                if (progresso > 100) progresso = 0;
                processBar.Value = progresso;
                if ((progresso % 10) == 0) processBar.Refresh();
            }
            listaMovto.Rows.Add(antBloco + "999999", "--", "----", "---------- ", "total",
                totRepo[0].ToString(), totRepo[1].ToString(), totRepo[2].ToString(), totRepo[3].ToString());
            listaMovto.Rows[listaMovto.Rows.Count - 1].DefaultCellStyle.BackColor = SystemColors.Info;
            listaMovto.Sort(movOrdem, ListSortDirection.Ascending);
            processBar.Visible = false;
        }

        private void menuMêsMenos_Click(object sender, EventArgs e)
        {
            menuMêsMais.Enabled = true;
            datada = datada.AddMonths(-1);
            difData = difData - 1;
            if (difData < -11) menuMêsMenos.Enabled = false;
            setBarra();
        }

        private void menuMêsMais_Click(object sender, EventArgs e)
        {
            menuMêsMenos.Enabled = true;
            datada = datada.AddMonths(1);
            difData = difData + 1;
            if (difData == 0) menuMêsMais.Enabled = false;
            setBarra();
        }

        private void menuPrint_Click(object sender, EventArgs e)
        {
            PrintDocument docto = new PrintDocument();
            qtItens = listaMovto.Rows.Count;
            docto.PrintPage += new PrintPageEventHandler(this.Docto_PrintPage);
            qRow = 0;
            docto.Print();
        }

        private void Docto_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Pen myPen = new Pen(Brushes.Black);
            Point pt1 = new Point(30, 53);
            Point pt2 = new Point(780, 53);
            ev.Graphics.DrawLine(myPen, pt1, pt2);
            Font myFont1 = new Font("Arial", 14);
            Font myFont2 = new Font("Lucida Console", 10);
            Font myFont3 = new Font("Lucida Sans", 10, FontStyle.Underline);
            Font myFont4 = new Font("Lucida Sans", 10);
            Font myFont5 = new Font("Lucida Sans", 8);
            string loja = "Parque das Águas Quentes";
            ev.Graphics.DrawString(loja, myFont1, Brushes.Black, 30, 30);
            ev.Graphics.DrawString("Atividades por Bloco no Mês", myFont1, Brushes.Black, 330, 30);
            if (qRow > 0)
            {
                nmPag = nmPag + 1;
                ev.Graphics.DrawString("pág. " + nmPag.ToString(), myFont1, Brushes.Black, 720, 50);
            }
            ev.Graphics.DrawString(datada.ToString("MMMM"), myFont1, Brushes.Black, 640, 30);
            ev.Graphics.DrawString(datada.ToString("/yyyy"), myFont1, Brushes.Black, 728, 30);
            ev.Graphics.DrawString("papel", myFont3, Brushes.Black, 548, 75);
            ev.Graphics.DrawString("sabo-", myFont3, Brushes.Black, 610, 75);
            ev.Graphics.DrawString("saco", myFont3, Brushes.Black, 668, 75);
            ev.Graphics.DrawString("rolo", myFont3, Brushes.Black, 728, 75);
            ev.Graphics.DrawString("Dia", myFont3, Brushes.Black, 30, 90);
            ev.Graphics.DrawString("Aptº", myFont3, Brushes.Black, 70, 90);
            ev.Graphics.DrawString("Camareira", myFont3, Brushes.Black, 120, 90);
            ev.Graphics.DrawString("Ação", myFont3, Brushes.Black, 444, 90);
            ev.Graphics.DrawString("higiên.", myFont3, Brushes.Black, 548, 90);
            ev.Graphics.DrawString("nete", myFont3, Brushes.Black, 615, 90);
            ev.Graphics.DrawString("lixo", myFont3, Brushes.Black, 678, 90);
            ev.Graphics.DrawString("papel", myFont3, Brushes.Black, 728, 90);
            bool halfLine = false;
            int yPos = 110;
            for (int nmLin = 0; nmLin < 58; nmLin++)
            {
                if (qRow >= qtItens) break;
                if (listaMovto.Rows[qRow].Cells[1].Value.ToString() == " ")
                {
                    if (qRow > 0)
                    {
                        yPos = yPos + 8;
                        if (halfLine)
                        {
                            nmLin = nmLin + 1;
                            halfLine = false;
                        }
                        else halfLine = true;
                    }
                }
                ev.Graphics.DrawString(listaMovto.Rows[qRow].Cells[2].Value.ToString(),
                    myFont4, Brushes.Black, 70, yPos);
                if (listaMovto.Rows[qRow].Cells[3].Value.ToString().Length > 33)
                    ev.Graphics.DrawString(listaMovto.Rows[qRow].Cells[3].Value.ToString(),
                    myFont5, Brushes.Black, 120, yPos);
                else ev.Graphics.DrawString(listaMovto.Rows[qRow].Cells[3].Value.ToString(),
                    myFont4, Brushes.Black, 120, yPos);
                ev.Graphics.DrawString(listaMovto.Rows[qRow].Cells[4].Value.ToString(),
                    myFont4, Brushes.Black, 444, yPos);
                string edçDia = listaMovto.Rows[qRow].Cells[1].Value.ToString();
                edçDia = edçDia.PadLeft(3);
                string edçPpHig = listaMovto.Rows[qRow].Cells[5].Value.ToString();
                edçPpHig = edçPpHig.PadLeft(5);
                string edçSabon = listaMovto.Rows[qRow].Cells[6].Value.ToString();
                edçSabon = edçSabon.PadLeft(5);
                string edçScLixo = listaMovto.Rows[qRow].Cells[7].Value.ToString();
                edçScLixo = edçScLixo.PadLeft(5);
                string edçPpRolo = listaMovto.Rows[qRow].Cells[8].Value.ToString();
                edçPpRolo = edçPpRolo.PadLeft(5);
                ev.Graphics.DrawString(edçDia, myFont2, Brushes.Black, 30, yPos);
                ev.Graphics.DrawString(edçPpHig, myFont2, Brushes.Black, 540, yPos);
                ev.Graphics.DrawString(edçSabon, myFont2, Brushes.Black, 600, yPos);
                ev.Graphics.DrawString(edçScLixo, myFont2, Brushes.Black, 660, yPos);
                ev.Graphics.DrawString(edçPpRolo, myFont2, Brushes.Black, 720, yPos);
                yPos = yPos + 16;
                qRow = qRow + 1;
            }
            if (qRow >= qtItens) ev.HasMorePages = false;
            else
            {
                ev.Graphics.DrawString("- - - - - - - - - - - - - - - - - - - -", myFont1, Brushes.Black, 200, yPos + 14);
                ev.HasMorePages = true;
            }
        }

        private void setBarra()
        {
            progresso = 0;
            processBar.Visible = true;
            processBar.Refresh();
            Monta_Lançto();
        }

        private void Starta_Tick(object sender, EventArgs e)
        {
            Starta.Enabled = false;
            setBarra();
        }
    }
}
